/**
 * 
 */
package GUI.GameBoard.Center;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.OverlayLayout;

import Monopol.Cubes.Cube;
import Utils.Utils;


//VS4E -- DO NOT REMOVE THIS LINE!
public class CenterPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private JPanel pnlWarning;
	private JPanel pnlSurprise;
	private JPanel pnlCubes;
	private JPanel pnlPicture;
	private BankPanel pnlBank;


	public CenterPanel() 
	{
		// Set the Layout
		setLayout(new OverlayLayout(this));
		// Make Transparent
		setOpaque(false);
		// Initialize all other Components
		initComponents();
		// Add the Center Picture
		add(getPnlPicture("/center.png"));
	}

	private void initComponents() {
		// Create a New Panel with GridBagLayout
		JPanel pnlMain = new JPanel(new GridBagLayout());
		// Make Transparent
		pnlMain.setOpaque(false);
		// Create Grid Bag Constrains (3 Rows, 3 Columns)
		GridBagConstraints topLeft = new GridBagConstraints(0, 0, 1, 1, 0.5, 0.5, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0);
		GridBagConstraints bottomRight = new GridBagConstraints(2, 2, 1, 1, 0.5, 0.5, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0);
		GridBagConstraints center = new GridBagConstraints(1, 1, 1, 1, 0.5, 0.5, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 5, 5);
		GridBagConstraints bottomLeft = new GridBagConstraints(0, 2, 2, 1, 1, 0.5, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 10), 0, 0);
		
	// Add the Components in Order
		
		// Add the Warning Cards at the Top Left
		pnlMain.add(getWarningPanel(), topLeft);
		// Add the Cubes to the Center
		pnlMain.add(getCubes(), center);
		// Add the Surprise Cards to the bottom right
		pnlMain.add(getSurprisePanel(), bottomRight);
		// Add the Bank Cash Panel
		pnlMain.add(getBankPanel(), bottomLeft);
		// Add the Panel to the Main Center Panel
		add(pnlMain);

	}
	
	
	/**
	 * @return
	 */
	private BankPanel getBankPanel() {
		if(pnlBank == null)
		{
			pnlBank = new BankPanel();
		}
		return pnlBank;
	}

	/**
	 * Create the Picture Panel
	 * @return The Picture Panel
	 */
	private JPanel getPnlPicture(String imagePath)
	{
		if(pnlPicture == null)
		{
			// Create a New Panel with Border Layout
			pnlPicture = new JPanel(new BorderLayout());
			// Set the Preferred Size
			pnlPicture.setPreferredSize(getPreferredSize());
			// Make transparent
			pnlPicture.setOpaque(false);
			// Get the Image
			ImageIcon image = Utils.getImageIcon(imagePath);
			// Check that the Image Exists
			if(image != null)
			{
				// Create the Image Label
				JLabel lblImage = new JLabel(Utils.getImageIcon(imagePath), JLabel.CENTER);
				// Add the Image Label to the PAnel
				pnlPicture.add(lblImage, BorderLayout.CENTER);
				// Add a Component Resize Listener
				pnlPicture.addComponentListener(new ResizeListener(pnlPicture,imagePath));
			}
		}
		return pnlPicture;
	}

	/**
	 * @return The Surprise Cards Panel
	 */
	private JPanel getSurprisePanel() {
		if(pnlSurprise == null)
		{
			pnlSurprise = new JPanel();
			pnlSurprise.setOpaque(false);
			pnlSurprise.addComponentListener(new ResizeListener(pnlSurprise, "/surprise_cards.png"));
			pnlSurprise.setToolTipText("Surprise Cards");
		}
		return pnlSurprise;
	}

	/**
	 * @return The Warning Cards Panel
	 */
	private JPanel getWarningPanel() {
		if(pnlWarning == null)
		{
			pnlWarning = new JPanel(); 
			pnlWarning.setOpaque(false);
			pnlWarning.addComponentListener(new ResizeListener(pnlWarning, "/warning_cards.png"));
			pnlWarning.setToolTipText("Warning Cards");
		}
		return pnlWarning;
	}

	/**
	 * @return the Cubes Panel
	 */
	private JPanel getCubes() {
		if(pnlCubes == null)
		{
			pnlCubes = new JPanel(); 
			pnlCubes.setOpaque(false);
		}
		return pnlCubes;
	}

	
	/**
	 * Adds the Game Cubes Picture to the Center Panel
	 * @param cubes The Game Cubes - Array of two Cubes
	 */
	public void setCubes(Cube[] cubes)
	{
		// Get the First Cube Label
		JLabel cube1 = cubes[0].getCubePanel();
		// Get the Second Cube Label
		JLabel cube2 = cubes[1].getCubePanel();
		// Clear the Cubes from the Screen
		pnlCubes.removeAll();
		// Add the Cubes to the Panel
		pnlCubes.add(cube1, CENTER_ALIGNMENT);
		pnlCubes.add(cube2, CENTER_ALIGNMENT);
		// Repaint and Validate the Panel
		pnlCubes.repaint();
		pnlCubes.validate();
	}
	
	/**
	 * Update the Cash Amount
	 * @param cash The Updated Cash Amount
	 */
	public void setCash(Double cash)
	{
		pnlBank.setCash(cash);
	}

	/**
	 * Resize listener to ensure image is sized correctly for the panel
	 *
	 */
	class ResizeListener extends ComponentAdapter {
		private JPanel panel;
		private String imagePath;
		
		/**
		 * @param resizePanel The Panel to be resized
		 * @param image The Image Path to resize
		 */
		public ResizeListener(JPanel resizePanel, String image)
		{
			panel = resizePanel;
			imagePath = image;
		}
		
		/**
		 * Overrides ComponentAdapter.componentResized
		 * Ensures the image is correctly resized
		 * @param evt the event
		 */
		public void componentResized(ComponentEvent evt) {
			// Notify that the Component Resized
			super.componentResized(evt);
			// Check if this Component is Our Panel
			if(evt.getComponent() instanceof JPanel) {
				// Clear the Panel
				panel.removeAll();
				// Create a new Resized Image	
				ImageIcon resizedIcon = Utils.createScaledIcon(imagePath, panel.getWidth(), panel.getHeight(), panel.getInsets());
				// Recreate the Picture Panel
				JLabel img = new JLabel(resizedIcon);
				panel.add(img, BorderLayout.CENTER);
				// Repaint and Validate
				Utils.repaintAndValidateAll(CenterPanel.this);
			}
		}
	}
}
